use std::{
     env, fs, io::{Read, Write}, os::unix::process::CommandExt, path::Path, process
};


// const PROJECT_VERSION: &str = "0.0.1";
// const PROJECT_NAME: &str = "ChatGLM.cpp";

fn main() {
    // let ggml_include_files = vec!["src/chatglm.cpp/third_party/ggml/include/ggml"];
    // let sentencepiece_include_files = vec![
    //     "src/chatglm.cpp/third_party/sentencepiece",
    //     "src/chatglm.cpp/third_party/sentencepiece/src",
    //     "src/chatglm.cpp/third_party/sentencepiece/src/builtin_pb",
    //     "src/chatglm.cpp/third_party/sentencepiece/third_party/absl/container",
    //     "src/chatglm.cpp/third_party/sentencepiece/third_party/absl/flags",
    //     "src/chatglm.cpp/third_party/sentencepiece/third_party/absl/memory",
    //     "src/chatglm.cpp/third_party/sentencepiece/third_party/absl/random",
    //     "src/chatglm.cpp/third_party/sentencepiece/third_party/absl/strings",
    //     "src/chatglm.cpp/third_party/sentencepiece/third_party/darts_clone",
    //     "src/chatglm.cpp/third_party/sentencepiece/third_party/esaxx",
    //     "src/chatglm.cpp/third_party/sentencepiece/third_party/protobuf-lite",
    //     "src/chatglm.cpp/third_party/sentencepiece/third_party/protobuf-lite/google/protobuf",
    //     "Vsrc/chatglm.cpp/third_party/sentencepiece/third_party/protobuf-lite/google/protobuf/io",
    //     "src/chatglm.cpp/third_party/sentencepiece/third_party/protobuf-lite/google/protobuf/stubs",
    // ];
    let orifp = Path::new("src/chatglm.cpp/chatglm.cpp1");
    if !orifp.exists() {
        fs::copy("src/chatglm.cpp/chatglm.cpp", "src/chatglm.cpp/chatglm.cpp1").unwrap();
        let mut orifp = fs::OpenOptions::new().read(true).open("src/chatglm.cpp/chatglm.cpp").unwrap();
        let mut ori = String::new();
        orifp.read_to_string(&mut ori).unwrap();
        ori = ori.replace("text = remove_special_tokens(text);", "//text = remove_special_tokens(text);");
        let mut orifp = fs::OpenOptions::new().write(true).append(false).open("src/chatglm.cpp/chatglm.cpp").unwrap();
        orifp.write_all(ori.as_bytes()).unwrap();
        //orifp.flush().unwrap();
    }

    // std::fs::copy("build/lib/libchatglm.a", format!("{}/libchatglm.a",out_dir)).unwrap();
    // std::fs::copy("build/lib/libchatglm.a", format!("{}/libchatglm.a",out_dir)).unwrap();
    // std::fs::copy("build/lib/libchatglm.a", format!("{}/libchatglm.a",out_dir)).unwrap();
    // std::fs::copy("build/lib/libchatglm.a", format!("{}/libchatglm.a",out_dir)).unwrap();
    let dst = cmake::Config::new("src/chatglm.cpp")
        .always_configure(true)
        .define("BUILD_SHARED_LIBS", "OFF")
        .build();
    // process::Command::new("bash")
    //     .arg("./build_with_cmake.sh")
    //     .spawn()
    //     .unwrap();
    // println!("cargo:rustc-link-lib=static={}", "build/lib/libchatglm.a");
    // println!(
    //     "cargo:rustc-link-lib=static={}",
    //     "build/lib/libsentencepiece.a"
    // );
    // println!(
    //     "cargo:rustc-link-lib=static={}",
    //     "build/lib/libsentencepiece_train.a"
    // );
    // let out_dir = env::var("OUT_DIR").unwrap();

    println!("cargo:rustc-link-search=native={}/lib", dst.display());
    //println!("cargo:rustc-link-search=native={}", "build/lib");
    //println!("cargo:rustc-link-lib=static={}", "chatglm");
    println!("cargo:rustc-link-lib=static={}", "sentencepiece");
    println!("cargo:rustc-link-lib=static={}", "sentencepiece_train");
    println!("cargo:rustc-link-search=native={}/lib/static", dst.display());
    println!("cargo:rustc-link-lib={}", "ggml");


    cxx_build::bridge("src/lib.rs")
        .flag("-std=c++17")
        .file("src/chatglm.cpp/chatglm.cpp")
        .file("src/extern.cpp")
        .include("src")
        .include("src/chatglm.cpp")
        .include("src/chatglm.cpp/third_party/ggml/include/ggml")
        .include("src/chatglm.cpp/third_party/sentencepiece/src")
        .flag("-w")
        .flag("-fpermissive")
        .static_flag(true)
        .compile("chatglm_cpp_sys");
}
